﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Controls.Primitives;

namespace Wintellect.LiveFx.Cloudboard
{
  public static class CueBannerService
  {
    //there is absolutely no way to write this statement out
    //to look pretty
    public static readonly DependencyProperty CueBannerProperty = DependencyProperty.RegisterAttached(
                                  "CueBanner",
                                  typeof(object), typeof(CueBannerService),
                                  new FrameworkPropertyMetadata(null, CueBannerPropertyChanged));

    public static object GetCueBanner(Control control)
    {
      return control.GetValue(CueBannerProperty);
    }

    public static void SetCueBanner(Control control, object value)
    {
      control.SetValue(CueBannerProperty, value);
    }

    private static void CueBannerPropertyChanged(DependencyObject d,
                                                 DependencyPropertyChangedEventArgs e)
    {
      Control control = (Control)d;
      control.Loaded += control_Loaded;
      if (d is ComboBox || d is TextBox || d is PasswordBox)
      {
        control.GotFocus += control_GotFocus;
        control.LostFocus += control_Loaded;
      }
    }

    private static void control_GotFocus(object sender, RoutedEventArgs e)
    {
      Control c = (Control)sender;
      if (ShouldShowCueBanner(c))
      {
        RemoveCueBanner(c);
      }
    }

    private static void control_Loaded(object sender, RoutedEventArgs e)
    {
      Control control = (Control)sender;
      if (ShouldShowCueBanner(control))
      {
        ShowCueBanner(control);
      }
    }

    private static void RemoveCueBanner(UIElement control)
    {
      AdornerLayer layer = AdornerLayer.GetAdornerLayer(control);

      Adorner[] adorners = layer.GetAdorners(control);
      if (adorners == null) return;
      foreach (Adorner adorner in adorners)
      {
        if (adorner is CueBannerAdorner)
        {
          adorner.Visibility = Visibility.Hidden;
          layer.Remove(adorner);
        }
      }
    }

    private static void ShowCueBanner(Control control)
    {
      AdornerLayer layer = AdornerLayer.GetAdornerLayer(control);
      Adorner cueBanner = new CueBannerAdorner(control, GetCueBanner(control));
      layer.Add(cueBanner);
    }

    private static bool ShouldShowCueBanner(Control c)
    {
      PasswordBox pb = c as PasswordBox;
      if (pb != null)
      {
        return String.IsNullOrEmpty(pb.Password);
      }

      DependencyProperty dp = GetDependencyProperty(c);
      if (dp == null) return true;
      object value = c.GetValue(dp);
      return (null == value || "".Equals(value));
    }

    private static DependencyProperty GetDependencyProperty(Control control)
    {
      if (control is ComboBox)
        return ComboBox.TextProperty;
      if (control is TextBoxBase)
        return TextBox.TextProperty;
      return null;
    }
  }
}
